package main

import (
	"flag"
	"fmt"
	"log"

	"server-go/rpc/reward/internal/config"
	"server-go/rpc/reward/internal/server"
	"server-go/rpc/reward/internal/svc"
	"server-go/rpc/reward/internal/util"
	"server-go/rpc/reward/types/reward"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

var configFile = flag.String("f", "etc/reward.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)
	// 开启队列
	func() {
		if err := ctx.ConsumerA.Start(); err != nil {
			log.Println("ConsumerA队列启动失败, " + err.Error())
			panic("ConsumerA队列启动失败, " + err.Error())
		}
		util.ReckConsumerA(ctx, ctx.ConsumerA)
	}()
	defer func() {
		ctx.ConsumerA.Shutdown()
	}()
	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		reward.RegisterRewardServer(grpcServer, server.NewRewardServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	err := consul.RegisterService(c.ListenOn, c.Consul)
	if err != nil {
		log.Println("服务注册失败")
		return
	}
	s.Stop()

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start()
}
